/**
 * @(#) Elettrodomestico.java
 */

package it.unibo.iss.domotica.edDomain;

import java.util.Properties;
import org.apache.log4j.Logger;

public class Elettrodomestico implements IElettrodomestico
{
	private String nome;	
	private StatoED stato;	
	private CatUsage catUso;	
	private CatConsumption catConsumption;
	private double consumoAttuale;
	
	// Logger
	private static final Logger logger = Logger.getLogger(Elettrodomestico.class);

	public Elettrodomestico() {
		this("", CatUsage.OPTIONAL, CatConsumptionEnum.LOW);
	}
	
	public Elettrodomestico(String nome, CatUsage catUso, CatConsumptionEnum catConsumption) {
		this.nome = nome;
		this.catUso = catUso;
		this.catConsumption = CatConsumption.getCatConsumption(catConsumption);
		this.stato = StatoED.OFF;
		this.consumoAttuale = 0;
	}
	
	public Elettrodomestico(String nome, CatUsage catUso, CatConsumptionEnum catConsumption, Properties prop) {
		this.nome = nome;
		this.catUso = catUso;
		this.catConsumption = CatConsumption.getCatConsumption(catConsumption, prop);
		this.stato = StatoED.OFF;
		this.consumoAttuale = 0;
	}
	
	public String getNome() {
		return nome;
	}
	
	public StatoED getStato() {
		return stato;
	}
	
	public CatUsage getCatUso() {
		return catUso;
	}
	
	public CatConsumptionEnum getCatConsumptionEnum() {
		return catConsumption.getCatConsumptionEnum();
	}
	
	public CatConsumption getCatConsumption() {
		return catConsumption;
	}
	
	public synchronized double getConsumption() {
		return consumoAttuale;
	}

	public synchronized void setConsumoAttuale(double consumoAttuale) {
		if(consumoAttuale >= 0)
			this.consumoAttuale = consumoAttuale;
	}

	@Override
	public boolean powerOn() {
		stato = StatoED.CONDITIONED;	
		
		return true;
	}

	@Override
	public boolean powerOff() {
		stato = StatoED.OFF;
		setConsumoAttuale(0);
		
		return true;
	}

	public double getMediumConsumption() {
		return (catConsumption.getMinConsumption() + catConsumption.getMaxConsumption()) / 2;
	}

	public void setStato(StatoED stato) {
		this.stato = stato;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Elettrodomestico other = (Elettrodomestico) obj;
		if (catConsumption.getCatConsumptionEnum() != other.catConsumption.getCatConsumptionEnum())
			return false;
		if (catUso != other.catUso)
			return false;
		if (Double.doubleToLongBits(consumoAttuale) != Double.doubleToLongBits(other.consumoAttuale))
			return false;
//		if (nome != other.nome)
//			return false;
		if (stato != other.stato)
			return false;
		if (!nome.equals(other.nome))
			return false;
		
		return true;
	}
	
	public boolean equals(String name) {
		return this.getNome().equals(name);
	}
	
	@Override
	public String toString() {
		return nome + "(" + stato + ", " + catUso + ", " + catConsumption.getCatConsumptionEnum() + ") - " + consumoAttuale;
	}

	@Override
	public boolean swithToActive() {
		if(stato == StatoED.CONDITIONED) {
			stato = StatoED.ACTIVE;
			return true;
		}
		return false;
	}
}
